/**
 * 
 */
package com.hexacta.hrs.domain.feedback;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;

import org.vectrics.common.db.AbstractRecordObject;
import org.vectrics.employee.Employee;
import org.vectrics.recruiter.job.JobFunction;

import com.hexacta.hrs.domain.feedback.effort.EffortScoreType;
import com.hexacta.hrs.domain.feedback.resultcalculations.Calculator;

/**
 * @author gvilla
 * 
 */
public class FeedbackPeriod extends AbstractRecordObject {

    private Employee employee;

    private Employee mentor;

    private boolean closed;

    private Period period;

    private ArchivoAdjunto discharge;

    private ArchivoAdjunto selfevaluation;

    private Set<Feedback> feedbacks;

    private FeedbacksDataSummary feedbacksDataSummary;

    private Calculator calculator;

    // Current band and position
    private JobFunction actualBand;

    private Long actualPosition;

    // Final effort, commitment, band and position, post management sanity check
    private JobFunction newFinalBand;

    private Long newFinalPosition;

    private String newPerformance;

    private String newCommitment;

    private FeedbackPeriod() {
        calculator = new Calculator();
        feedbacks = new HashSet<Feedback>();
        closed = false;

    }
    
    /**
     * Este metodo no se ha utilizado;
 	 *
     *	private String currentPeriod() {
     *  	Calendar cal = new GregorianCalendar();
     *   	String year = String.valueOf(cal.get(Calendar.YEAR));
     *  return year;
     *  }    
    */

    /**
     * @param begin
     * @param end
     */
    public FeedbackPeriod(final Period period, final JobFunction actualBand, final Long actualPosition) {
        this();
        this.period = period;
        this.actualBand = actualBand;
        this.actualPosition = actualPosition;
    }

    /**
     * @return the feedbacks
     */
    public Set<Feedback> getFeedbacks() {
        return feedbacks;
    }

    /**
     * @param feedbacks
     *            the feedbacks to set
     */
    public void setFeedbacks(final Set<Feedback> feedbacks) {
        this.feedbacks = feedbacks;
    }

    /**
     * @return the closed
     */
    public boolean isClosed() {
        return closed;
    }

    /**
     * @param closed
     *            the closed to set
     */
    public void setClosed(final boolean closed) {
        this.closed = closed;
    }

    /**
     * @return the period
     */
    public Period getPeriod() {
        return period;
    }

    /**
     * @param period
     *            the period to set
     */
    public void setPeriod(final Period period) {
        this.period = period;
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee
     *            the employee to set
     */
    public void setEmployee(final Employee employee) {
        this.employee = employee;
    }

    public Employee getMentor() {
        return mentor;
    }

    public void setMentor(final Employee mentor) {
        this.mentor = mentor;
    }

    /**
     * @return the discharge
     */
    public ArchivoAdjunto getDischarge() {
        return discharge;
    }

    /**
     * @param discharge
     *            the discharge to set
     */
    public void setDischarge(final ArchivoAdjunto discharge) {
        this.discharge = discharge;
    }

    /**
     * @return the selfevaluation
     */
    public ArchivoAdjunto getSelfevaluation() {
        return selfevaluation;
    }

    /**
     * @param selfevaluation
     *            the selfevaluation to set
     */
    public void setSelfevaluation(final ArchivoAdjunto selfevaluation) {
        this.selfevaluation = selfevaluation;
    }

    /**
     * @param feedback
     */
    public void addFeedback(final Feedback feedback) {

        feedbacks.add(feedback);
    }

    public FeedbacksDataSummary getFeedbacksDataSummary() {
        return feedbacksDataSummary;
    }

    public void setFeedbacksDataSummary(final FeedbacksDataSummary feedbacksDataSummary) {
        this.feedbacksDataSummary = feedbacksDataSummary;
    }

    public JobFunction getActualBand() {
        return actualBand;
    }

    public void setActualBand(final JobFunction actualBand) {
        this.actualBand = actualBand;
    }

    public Long getActualPosition() {
        return actualPosition;
    }

    public void setActualPosition(final Long actualPosition) {
        this.actualPosition = actualPosition;
    }

    public JobFunction getNewFinalBand() {
        return newFinalBand;
    }

    public void setNewFinalBand(final JobFunction newFinalBand) {
        this.newFinalBand = newFinalBand;
    }

    public Long getNewFinalPosition() {
        return newFinalPosition;
    }

    public String getDisplayableNewFinalPosition() {
        if (newFinalPosition == null)
            return "";
        return newFinalPosition.toString() + "%";
    }

    public void setNewFinalPosition(final Long newFinalPosition) {
        this.newFinalPosition = newFinalPosition;
    }

    public String getNewBand() throws ParseException {

        String finalScore = feedbacksDataSummary.getHrsFinalScore();
        if (finalScore.equals("-"))
            return "-";

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        Date finish = sdf.parse("31/12/" + period.getYear());
        Date start = sdf.parse("01/01/" + period.getYear());

        // Si empezo a trabajar despues del 01 de enero, se toma la fecha real
        // de inicio.
        if (employee.getFirstEmployeeDate().after(start)) {
            start = employee.getFirstEmployeeDate();
        }

        String newBand = calculator.getNewBand(employee.getFunction().getDescription(),
                employee.getCareerRolePosition(), start, finish, finalScore);
        return newBand;
    }

    public String getNewPosition() throws ParseException {

        String finalScore = feedbacksDataSummary.getHrsFinalScore();
        if (finalScore.equals("-"))
            return "-";

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        Date finish = sdf.parse("31/12/" + period.getYear());
        Date start = sdf.parse("01/01/" + period.getYear());

        // Si empezo a trabajar despues del 01 de enero, se toma la fecha real
        // de inicio.
        if (employee.getFirstEmployeeDate().after(start)) {
            start = employee.getFirstEmployeeDate();
        }

        Long newPosition = calculator.getNewPosition(employee.getFunction().getDescription(),
                employee.getCareerRolePosition(), start, finish, finalScore);

        return newPosition.toString() + " %";
    }

    public String getPromoted() throws ParseException {
        String newBand = getNewBand();
        if (newBand.equalsIgnoreCase("-"))
            return "-";
        if (!employee.getFunction().getDescription().equalsIgnoreCase(newBand))
            return "Yes";
        else
            return "No";
    }

    public String getOutstanding() {
        String finalEffort = getFeedbacksDataSummary().getHrsFinalEffort();
        String finalScore = getFeedbacksDataSummary().getHrsFinalScore();

        if (finalEffort.equalsIgnoreCase("-") || finalScore.equalsIgnoreCase("-"))
            return "-";
        if (finalEffort.equalsIgnoreCase(EffortScoreType.Sobresaliente.name())
                && finalScore.equalsIgnoreCase(ScoreType.EE.getDescription()))
            return "Yes";
        else
            return "No";

    }

    /**
     * @return the newCommitment
     */
    public String getNewCommitment() {
        return newCommitment;
    }

    /**
     * @return the newEffort
     */
    public String getNewPerformance() {
        return newPerformance;
    }

    /**
     * @param newEffort
     *            the newEffort to set
     */
    public void setNewPerformance(final String newEffort) {
        newPerformance = newEffort;
    }

    /**
     * @param newCommitment
     *            the newCommitment to set
     */
    public void setNewCommitment(final String newCommitment) {
        this.newCommitment = newCommitment;
    }
}
